import pygame.mixer

from tools.water_pipe import *
from tools.hole import *
from enemies.goomba import *

goomba_group = [GoomBa((700, 404 - 38)), GoomBa((889 + 320 + 76, 404 - 38))]
goomba_bg_move = 0


def setup_pipe(bg_width, bg_move, mario):
    pipe1 = WaterPipe(889, 404 - 76, 76, 76)  # 第一个管道
    pipe2 = WaterPipe(889 + 320, 404 - 110, 76, 110)
    pipe3 = WaterPipe(889 + 580, 440 - 175, 76, 175)
    pipe4 = WaterPipe(889 + 930, 440 - 175, 76, 175)
    pipe5 = WaterPipe(889 + 4320, 404 - 76, 76, 76)
    pipe6 = WaterPipe(889 + 4320 + 517, 404 - 76, 76, 76)
    pipe_group = pygame.sprite.Group()
    pipe_group.add(pipe1)
    pipe_group.add(pipe2)
    pipe_group.add(pipe3)
    pipe_group.add(pipe4)
    pipe_group.add(pipe5)
    pipe_group.add(pipe6)
    for i in pipe_group:
        if bg_move + bg_width > i.rect.x:
            i.rect.x = bg_width - bg_move + (i.rect.x - bg_width)
            res = pygame.sprite.collide_rect(i, mario)
            mario.min_y = i.rect.y
            if res:
                if i.rect.x < mario.rect.left + mario.width and i.rect.y < mario.rect.y - \
                        mario.height and i.rect.right > mario.rect.x + 10:  # 左碰撞检测
                    mario.rect.right = i.rect.x
                    mario.is_jump = True
                    # print("碰撞1")
                elif i.rect.y >= mario.rect.y:  # 上碰撞检测
                    mario.rect.y = i.rect.y - mario.height
                    mario.is_pipe = True
                    mario.is_jump = False
                    # print("碰撞3")
                elif i.rect.x + i.rect.width > mario.rect.x and i.rect.y < mario.rect.y \
                        - mario.height:  # 右碰撞检测
                    mario.rect.x = i.rect.x + i.rect.width
                    # print("碰撞2")
                    mario.is_jump = True
            else:
                mario.is_pipe = False


def setup_hole(bg_width, bg_move, mario):
    hole_group = pygame.sprite.Group()
    hole_group.add(Hole(889 + 1340, 405 - 5, 20, 45))
    hole_group.add(Hole(889 + 1890, 405 - 5, 45, 45))
    hole_group.add(Hole(889 + 4040, 405 - 5, 20, 45))
    for i in hole_group:
        if bg_move + bg_width > i.rect.x:
            i.rect.x = bg_width - bg_move + (i.rect.x - bg_width)
            res = pygame.sprite.collide_rect(i, mario)
            mario.min_y = 448 - mario.height
            if res:
                mario.is_down = True
                if mario.death_jump_down is False and mario.is_death is False and mario.rect.y + mario.height \
                        > i.rect.y + i.rect.height:
                    mario.mario_death()
                elif mario.is_death is True:
                    mario.mario_death()


def setup_goomba(screen, mario, bg_width, bg_move):
    for goomba in goomba_group:
        if bg_move + bg_width > goomba.rect.x:
            if mario.rect.x > bg_width / 2:
                goomba.bg_move += mario.speed
            goomba.alive = True
            goomba.now_x += goomba.speed
            goomba.rect.x = bg_width - goomba.now_x - goomba.bg_move
            res = pygame.sprite.collide_rect(goomba, mario)
            if goomba.is_death is False:
                goomba.run()
            if goomba.is_death is True:
                goomba.is_death_num += 1
                if goomba.is_death_num > 20:
                    goomba.rect.x = -50
            if res and goomba.is_death is False and mario.is_death is False:
                if mario.rect.bottom - 10 < goomba.rect.top and goomba.is_death is False and mario.is_down is False:
                    goomba.death()
                    jump_sound = pygame.mixer.Sound("sound/small_jump.ogg")
                    jump_sound.play()
                    mario.vel_y = -18
                    mario.is_jump = True
                else:
                    mario.is_down = True
                    if mario.death_jump_down is False and mario.is_death is False and mario.rect.y + mario.height \
                            > goomba.rect.y + goomba.rect.height:
                        mario.mario_death()
                    elif mario.is_death is True:
                        mario.mario_death()
            screen.blit(goomba.image, goomba.rect)
